package config

import (
	"context"
	schema "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/config"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type ConfigInoutTypeLog struct {
	ctx context.Context
	db  *gorm.DB
	logx.Logger
}

func NewConfigInoutTypeLog(ctx context.Context, db *gorm.DB) *ConfigInoutTypeLog {
	return &ConfigInoutTypeLog{
		ctx:    ctx,
		db:     db,
		Logger: logx.WithContext(ctx),
	}
}

// Add 创建一条日志
func (l *ConfigInoutTypeLog) Add(data schema.ConfigInoutTypeLog) (id int64, err error) {
	tx := l.db.Create(&data)
	if tx.Error != nil {
		err = tx.Error
		return 0, err
	}
	return data.ID, nil
}

// GetPageList 获取分页列表
func (l *ConfigInoutTypeLog) GetPageList(page int64, pageSize int64) (list []schema.ConfigInoutTypeLog, err error) {
	list = make([]schema.ConfigInoutTypeLog, 0)
	ret := l.db.Offset(int((page - 1) * pageSize)).Limit(int(pageSize)).Order("created_at desc").Find(&list)
	if ret.Error != nil {
		err = ret.Error
		return
	}
	return list, nil
}

// GetTotal 获取总数
func (l *ConfigInoutTypeLog) GetTotal() (total int64, err error) {
	ret := l.db.Model(&schema.ConfigInoutTypeLog{}).Count(&total)
	if ret.Error != nil {
		err = ret.Error
		return
	}
	return
}
